<template>
  <!-- 作用：上传头像 -->
  <div class="upload-img">
    <el-upload :before-upload="beforeUpload" class="upload" action="#" :show-file-list="false" :http-request="httpRequest">
      <img v-if="value" v-errorimg class="img" :src="value" alt="">
      <i v-else class="el-icon-plus icon" />
    </el-upload>
    <!-- 删除 -->
    <div v-if="value" class="mask" />
    <i v-if="value" class="el-icon-delete del " @click="delEvent" />
    <!-- 预览 -->
    <i v-if="value" class="el-icon-zoom-in big" @click="show=true" />
    <!-- 弹窗 -->
    <el-dialog title="图片预览" :visible.sync="show">
      <img v-errorimg :src="value" class="big-img" alt="">
    </el-dialog>
    <!-- 进度值 -->
    <el-progress v-if="showProgress" :width="198" class="progress" type="circle" :percentage="percent" />
  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5'
// SECRETID 和 SECRETKEY 请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
var cos = new COS({
  SecretId: 'AKIDNXR8bpeSF1JL8EyZopxRfdHgqGUeuEn8',
  SecretKey: 'HJ9mAbQEn7jgjY9swEI6yAamk7MN8hty'
})
export default {
  name: 'UploadImg', // 1:方便调试 2:keep-alive include='组件的name'  3:路由跳转 this.$router.push({name:'Xxx'}) 4:使用导入组件name进行组件注册
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      imgUrl: '',
      show: false,
      percent: 0,
      showProgress: false
    }
  },
  created() {
    const obj = { num: 1 }
    console.log(JSON.stringify(obj))
    obj.num = 2
    console.log(obj)
    obj.num = 3
  },
  methods: {
    httpRequest(res) {
      // res.file
      this.showProgress = true
      cos.uploadFile({
        Bucket: 'abc-1305249343', /* 存储桶名称 */
        Region: 'ap-chongqing', /* 存储桶所在地域，必须字段 */
        Key: '96/' + res.file.name, /* 存储在桶里的对象键（例如:1.jpg，a/b/test.txt，图片.jpg）支持中文，必须字段 */
        Body: res.file, // 上传文件对象
        SliceSize: 1024 * 1024 * 0.5, /* 触发分块上传的阈值，超过5MB使用分块上传，小于5MB使用简单上传。可自行设置，非必须 */
        onProgress: (progressData) => {
          this.percent = +(progressData.percent * 100).toFixed(2)
          if (progressData.percent === 1) {
            this.showProgress = false
          }
          console.log(JSON.stringify(progressData))
        }
      }, (err, data) => {
        if (err) {
          console.log('上传失败', err)
        } else {
        //   this.imgUrl = 'http://' + data.Location
          this.$emit('input', 'http://' + data.Location)
          console.log('上传成功', this.value)
        }
      })
    },
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG,PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    delEvent() {
      this.$emit('input', '')
    }
  }
}
</script>
<style lang="scss" scoped>
.upload{
    width:200px;
    height:200px;
    border:1px dashed #4da4ff;
    display: flex;
    align-items: center;
    justify-content: center;
    .img{
      width:200px;
      height:200px;
      object-fit: contain;
    }
    .icon{
        font-size: 30px;
    }

}
.upload-img{
    position: relative;
    width:200px;
    line-height: 0;
    .del,.big{
        cursor: pointer;
        position: absolute;
        top:50%;
        left:110px;
        font-size: 24px;
        transform: translateY(-50%);
        color:#fff;
        display: none;
    }
    .big{
        left:70px;
    }
    .mask{
        width:200px;
        height:200px;
        position: absolute;
        top:0;
        left:0;
        background-color: rgba(0,0,0,0.3);
        display: none;
    }
    &:hover{
        .mask{
            display: block;
        }
        .del,.big{
            display: block;
        }
    }
    .big-img{
        width: 100%;
        height: 100%;
        object-fit: contain;
    }
    .progress{
      position: absolute;
      left:1px;
      top:1px;
      background-color: #fff;
    }
}
</style>
